﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrameWork.Interface;
using FrameWork.Components;
using Slja.Utility;
using Slja.Model;
using Slja.BLL;
using Slja.Model.Platform;

namespace FrameWork.web.Web.UC
{
    public partial class WorkTeam : OrderSectionBase
    {
     
        protected override void  LoadData()
        {
 	        base.LoadData();
            List<OrderWorkTeamModel> li = new OrderWorkTeamBLL().GetWorkersList(Order.ID);
            //绑定设计
            OrderWorkTeamModel designer = li.Find(delegate(OrderWorkTeamModel m) { return m.WorkRole == (int)WorkerRole.OrderDesigner; });
            if (designer != null)
            {
                
                hdfDesigner.Value = designer.WorkerID.ToString();
                hdfWorkTBLID_Desi.Value = designer.ID.ToString();
                tlbDesigner.Text = designer.WorkerName;
                tlbDesignerNote.Text = designer.Note;
            }
            //绑定合约
            OrderWorkTeamModel contracter = li.Find(delegate(OrderWorkTeamModel m) { return m.WorkRole == (int)WorkerRole.OrderContracter; });
            if (contracter != null)
            {
                hdfContractor.Value = contracter.WorkerID.ToString();
                hdfWorkTBLID_Cont.Value = contracter.ID.ToString();
                tlbContractor.Text = contracter.WorkerName;
                tlbContractorNote.Text = contracter.Note;
            }
            //绑定工程助理
            OrderWorkTeamModel fixassistant= li.Find(delegate(OrderWorkTeamModel m) { return m.WorkRole == (int)WorkerRole.FixAssistant; });
            if (fixassistant != null)
            {
                hdfFixAssistant.Value = fixassistant.WorkerID.ToString();
                hdfWorkTBLID_FixAs.Value = fixassistant.ID.ToString();
                tlbFixAssistant.Text = fixassistant.WorkerName;
                tlbFixAssistantNote.Text = fixassistant.Note;
            }
            else //默认：褚燕云是工程助理(31,
            {
                UserModel user = WebCommon.GetDefaultFixAssistant();
                hdfFixAssistant.Value = user.UserID.ToString();
                hdfWorkTBLID_FixAs.Value = "";
                tlbFixAssistant.Text = user.U_CName;
                tlbFixAssistantNote.Text = "";
            }
        }
        protected override OpRightState GetOpRight()
        {
            OpRightState opright = OpRightState.Readonly;
            if (Order.Enum_BackState == SaleState.AssignWork && //退回到 工作分配
               Order.CurOpUserID == Common.Get_UserID)
            {
                opright= OpRightState.Editable;
            }
            //未到”工作分配”
            if (Order.SaleStateLog < (int)SaleState.NewOrder)
            {
                opright = OpRightState.Hidden;
            }
            //正待”工作分配”
            else if ((Order.SaleStateLog == (int)SaleState.NewOrder))//刚刚下单               
            {//检查当前用户
                if (Order.CurOpUserID == Common.Get_UserID)
                {
                    opright = OpRightState.Editable;
                }
                else
                {
                    opright = OpRightState.Hidden;
                }
            }
            else //已”工作分配”
            {
                opright = OpRightState.Readonly;
            }

            //针对打印，处理:编辑变为不可编辑;
            if (ControlUsage == DisplayUsage.PrintDisplay && opright == OpRightState.Editable)
            {
                opright = OpRightState.Readonly;
            }
            return opright;
        }
        public override void SaveData()
        {
            OrderWorkTeamBLL bll = new OrderWorkTeamBLL();
            //保存设计
            SaveDesi(bll);
            //保存合约
            SaveCont(bll);
            //保存工程助理
            SaveFixAss(bll);
        }
        /// <summary>
        /// 设计ID
        /// </summary>
        public int DesignerID
        {
            get { return int.Parse(hdfDesigner.Value); }
        }
        /// <summary>
        /// 设计Name
        /// </summary>
        public string DesignerName
        {
            get { return tlbDesigner.Text; }
        }
        /// <summary>
        /// 合约ID
        /// </summary>
        public int ContractorID
        {
            get { return int.Parse(hdfContractor.Value); }
        }
        /// <summary>
        /// 合约Name
        /// </summary>
        public string ContractorName
        {
            get { return tlbContractor.Text; }
        }
        private void SaveDesi(OrderWorkTeamBLL bll)
        {
            OrderWorkTeamModel desi = new OrderWorkTeamModel();
            desi.CreateTime = DateTime.Now;
            desi.Note = tlbDesignerNote.Text.Trim();
            desi.OrderID = Order.ID;
            desi.WorkerID = int.Parse(hdfDesigner.Value);
            desi.WorkerName = tlbDesigner.Text;
            desi.WorkRole = (int)WorkerRole.OrderDesigner;
            if (hdfWorkTBLID_Desi.Value.Trim() == "")
            {
                bll.Add(desi);
            }
            else
            {
                desi.ID = int.Parse(hdfWorkTBLID_Desi.Value);
                bll.Update(desi);
            }
        }
        private void SaveCont(OrderWorkTeamBLL bll)
        {
            OrderWorkTeamModel Cont = new OrderWorkTeamModel();
            Cont.CreateTime = DateTime.Now;
            Cont.Note = tlbContractorNote.Text.Trim();
            Cont.OrderID = Order.ID;
            Cont.WorkerID = int.Parse(hdfContractor.Value);
            Cont.WorkerName = tlbContractor.Text;
            Cont.WorkRole = (int)WorkerRole.OrderContracter;
            if (hdfWorkTBLID_Cont.Value.Trim() == "")
            {
                bll.Add(Cont);
            }
            else
            {
                Cont.ID = int.Parse(hdfWorkTBLID_Cont.Value);
                bll.Update(Cont);
            }
        }
        private void SaveFixAss(OrderWorkTeamBLL bll)
        {
            OrderWorkTeamModel FixAss = new OrderWorkTeamModel();
            FixAss.CreateTime = DateTime.Now;
            FixAss.Note = tlbFixAssistantNote.Text.Trim();
            FixAss.OrderID = Order.ID;
            FixAss.WorkerID = int.Parse(hdfFixAssistant.Value);
            FixAss.WorkerName = tlbFixAssistant.Text.Trim();
            FixAss.WorkRole = (int)WorkerRole.FixAssistant;
            if (hdfWorkTBLID_FixAs.Value.Trim() == "")
            {
                bll.Add(FixAss);
            }
            else
            {
                FixAss.ID = int.Parse(hdfWorkTBLID_Cont.Value);
                bll.Update(FixAss);
            }
        }
    }
}